﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;

namespace HY.WEB.SA
{
    public partial class jobs_edit : PageBase
    {

        string sid = "";
        public string us,um,tab="comp_job";
        public BLL.CO.jobs jbs = new BLL.CO.jobs();

        protected void Page_Load(object sender, EventArgs e)
        {
            us = Session["ID"].ToString();
            um = Session["user"].ToString();
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request["sid"]))
                {
                    sid = Request["sid"].Trim();

                    hidsid.Text = sid;
                    fill();
                    BindText(sid);
                }
            }

        }

        void fill()
        {

            //公司分类
            DataTable e = BLL.Bllbase.get_table("[company]");
            ddlcompany.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlcompany.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["cname"].ToString().Trim(), e.Rows[i]["cbh"].ToString().Trim()));
            }
            ddlcompany.DataBind();

            //岗位分类
            e = BLL.Bllbase.get_table("[pub_cate]");
            //  e = BLL.Bllbase.get_table("[pub_cate_sub]", " right(rtrim(scode)='00'");
            ddlgw.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlgw.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["svalue"].ToString().Trim(), e.Rows[i]["scode"].ToString().Trim()));
            }
            ddlgw.DataBind();

            //工作地点
            e = BLL.Bllbase.get_table("[pub_area]");
            ddlSheng.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlSheng.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["svalue"].ToString().Trim(), e.Rows[i]["scode"].ToString().Trim()));
            }
            ddlSheng.DataBind();


            //镇区
            e = BLL.Bllbase.get_table("[pub_zone]");
            ddlzone.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlzone.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["svalue"].ToString().Trim(), e.Rows[i]["scode"].ToString().Trim()));
            }
            ddlzone.DataBind();



            //学历
            e = BLL.Bllbase.get_table("[sys_edu]");
            ddledu.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddledu.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["edu_nm"].ToString().Trim(), e.Rows[i]["edu_nm"].ToString().Trim()));
            }
            ddledu.DataBind();

            DataTable d = jbs.GetList("sid=" + sid + "").Tables[0];

            //行业分类
            ddlcompany.SelectedIndex = ddlcompany.Items.IndexOf(ddlcompany.Items.FindByValue(d.Rows[0]["cbh"].ToString()));
            //客户分类
            ddlgw.SelectedIndex = ddlgw.Items.IndexOf(ddlgw.Items.FindByValue(d.Rows[0]["cate"].ToString()));

            //客户分类
            ddlSheng.SelectedIndex = ddlSheng.Items.IndexOf(ddlSheng.Items.FindByValue(d.Rows[0]["area"].ToString()));

            //客户性质
            ddlzone.SelectedIndex = ddlzone.Items.IndexOf(ddlzone.Items.FindByValue(d.Rows[0]["zone"].ToString()));

            ddledu.SelectedIndex = ddledu.Items.IndexOf(ddledu.Items.FindByValue(d.Rows[0]["edu"].ToString()));

            ddlsex.SelectedIndex = ddlsex.Items.IndexOf(ddlsex.Items.FindByValue(d.Rows[0]["sex"].ToString()));

          
        }

        void BindText(string sid)
        {
            DataTable dt = jbs.GetList("sid=" + sid).Tables[0];

            if (dt.Rows.Count > 0)
            {
                hidsid.Text = dt.Rows[0]["sid"].ToString();
                txtjob.Text = dt.Rows[0]["title"].ToString();
                hidbh.Text = dt.Rows[0]["jbh"].ToString();

                txtjobday.Text = dt.Rows[0]["jobday"].ToString();
                txtage.Text = dt.Rows[0]["age"].ToString();


                string rdate = dt.Rows[0]["stdate"].ToString().Trim();
                if (rdate.Substring(0, 4) != "1900")
                {
                  DateTime   lastday = DateTime.Parse(rdate);
                  date1.Text = lastday.ToString("yyyy-M-d");
                }


                rdate = dt.Rows[0]["edate"].ToString().Trim();
                if (rdate.Substring(0, 4) != "1900")
                {
                    DateTime lastday = DateTime.Parse(rdate);
                    date2.Text = lastday.ToString("yyyy-M-d");
                }

                ddlgwsub.Items.Add(new ExtAspNet.ListItem(get_cate(dt.Rows[0]["cate_sub"].ToString().Trim()), dt.Rows[0]["cate_sub"].ToString().Trim()));

                ddlShi.Items.Add(new ExtAspNet.ListItem(get_area(dt.Rows[0]["area_sub"].ToString().Trim()), dt.Rows[0]["area_sub"].ToString().Trim()));
       

                txtkey.Text = dt.Rows[0]["keys"].ToString();

                txtmoney.Text = dt.Rows[0]["moneys"].ToString();

                txtnum.Text = dt.Rows[0]["pnum"].ToString();

                FCKeditor1.Value = HttpUtility.HtmlDecode(dt.Rows[0]["remarker"].ToString());
            
            }

        }

     public string  get_cate(string val)
       {
      return  BLL.Bllbase.get_val("pub_cate_sub","svalue","scode='"+val+"'");       
       
       }

     public string get_area(string val)
     {
         return BLL.Bllbase.get_val("pub_area_sub", "svalue", "scode='" + val + "'");

     }
        protected void ddlSheng_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlShi.Items.Clear();
            BindShi();
        }



        private void BindShi()
        {
            string sheng = ddlSheng.SelectedValue;
            //工作地点
            DataTable e = BLL.Bllbase.get_table("[pub_area_sub]", "left(scode,4)='" + sheng + "'");
            ddlShi.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlShi.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["svalue"].ToString().Trim(), e.Rows[i]["scode"].ToString().Trim()));
            }
            ddlShi.DataBind();


            //  ddlShi.Items.Insert(0, new ListItem("选择地区市", "-1"));
        }

        protected void ddlgw_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlgwsub.Items.Clear();
            BindGW();
        }

        private void BindGW()
        {
            string gw = ddlgw.SelectedValue.Trim();
            gw = gw.Substring(0, 2);
            //工作地点
            DataTable e = BLL.Bllbase.get_table("pub_cate_sub", "left(scode,2)='" + gw + "'");
            ddlgwsub.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlgwsub.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["svalue"].ToString(), e.Rows[i]["scode"].ToString()));
            }
            ddlgwsub.DataBind();
            //  ddlShi.Items.Insert(0, new ListItem("选择地区市", "-1"));
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Model.CO.jobs jb = new Model.CO.jobs();

            jb.jbh = hidbh.Text; //岗位编号
            if (jb.jbh == "") return;

            jb.sid =int.Parse(hidsid.Text);
            jb.cbh = ddlcompany.SelectedValue; //公司编号
            jb.cname = ddlcompany.SelectedText; //公司名称

            jb.title = txtjob.Text; //岗位

            jb.cate = ddlgw.SelectedValue; //岗位
            jb.cate_sub = ddlgwsub.SelectedValue; //子岗位


            jb.area = ddlSheng.SelectedValue; //省
            jb.area_sub = ddlShi.SelectedValue; //市
            jb.zone = ddlzone.SelectedValue;
            jb.jobday = int.Parse(txtjobday.Text);
            jb.age = int.Parse(txtage.Text);


            jb.stdate = DateTime.Parse(date1.Text.Trim());
            jb.edate = DateTime.Parse(date2.Text.Trim());
            jb.sex = ddlsex.SelectedValue;
            jb.keys = txtkey.Text;
            jb.moneys = decimal.Parse(txtmoney.Text);
            jb.pnum = int.Parse(txtnum.Text);

            jb.edu = ddledu.SelectedValue;



            jb.remarker = HttpUtility.HtmlEncode(FCKeditor1.Value);

            BLL.CO.jobs js = new BLL.CO.jobs();
            js.Update(jb);
          
            //更新业务员操作记录
            BLL.Bllbase.update_rec(tab, um, " jbh='" + jb.jbh + "' ");


            Alert.Show("修改完成 ");
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
       
        }

       
    }
}